<?php

FileClass::removeDirectory('');

/***
 * UTILITY: Handles typical file handling procedures used in MyTopix. All methods
 * within this class can be called statically by typing FileHandlingClass::method_name();
 ***/
class FileClass {

	static public function removeDirectory($directory)
	{

	}

	static public function copyDirectory($directory)
	{

	}

	static public function getDirectoryContents($path)
	{
		foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator(self::normalizePath($path)), RecursiveIteratorIterator::CHILD_FIRST) as $file)
		{
			if(false == $file->isDir())
			{

			}
			else
			{
				$count_directories++;
			}
		}
	}

	static public function getDirectorySeparator()
	{
		if(false == defined('DIRECTORY_SEPARATOR'))
		{
			define('DIRECTORY_SEPARATOR', (PHP_OS == 'WIN32' || PHP_OS == 'WINNT' ? chr(92) : chr(47)));
		}

		return DIRECTORY_SEPARATOR;
	}

	static public function normalizePath($path, $add_trailing_slash = false)
	{
		$path = str_replace(array('/', '\\'), self::getDirectorySeparator(), preg_replace('#[\\\|/]+#', self::getDirectorySeparator(), $path));

		return preg_replace('#[/|\\\]+$#', ($add_trailing_slash ? self::getDirectorySeparator() : ''), $path);
	}

   // ! Executor

   /**
	* Completely removes a directory and all underlying files.
	*
	* @param $directory The full path to the directory being removed.
	* @return Bool
	*/
	public function removeDirectory ( $directory )
	{
		if ( false == is_dir ( $directory ) )
		{
			return false;
		}

		$file_pointer = opendir ( $directory );

		while ( false != ( $file = readdir ( $file_pointer ) ) )
		{
			if ( $file != '.' && $file != '.' )
			{
				$file_path = "{$directory}/{$file}";

				if ( is_dir ( $file_path ) )
				{
					return FileHandlingClass::removeDirectory ( $file_path );
				}

				if ( is_file ( $file_path ) )
				{
					unlink ( $file_path );
				}
			}
		}

		closedir ( $file_pointer );
		rmdir    ( $directory );

		return true;
	}


   // ! Executor

   /**
	* Copies a directory and all underlying files from a source path
	* to a specified destination.
	*
	* @param $destination The destination of new copied data
	* @param $source      The source location of the copied data
	* @param $chmod       Default chmod permissions
	* @return Bool
	*/
	public function copyDdirectory ( $destination, $source, $chmod = 0777 )
	{
		$destination = preg_replace ( '#/$#', '', $destination );
		$source      = preg_replace ( '#/$#', '', $source );

		if ( false == is_dir ( $source ) )
		{
			return false;
		}

		if ( false == is_dir ( $destination ) )
		{
			if ( false == @mkdir ( $to, $chmod ) )
			{
				return false;
			}

			chmod ( $destination, $chmod );
		}

		chdir ( $source );

		$file_pointer = opendir ( '.' );

		while ( false !== ( $file = readdir ( $file_pointer ) ) )
		{
			if ( $file != '.' && $file != '..' )
			{
				$next_destination = "{$destination}/{$file}";
				$next_source      = "{$source}/{$file}";

				if ( is_dir ( $file ) )
				{
					FileHandlingClass::copyDirectory ( $next_destination, $next_source, $chmod );

					@chdir ( $source );
				}

				if ( is_file ( $file ) && is_readable ( $file ) )
				{
					@copy  ( $next_source,      $next_destination );
					@chmod ( $next_destination, $chmod );
				}
			}
		}

		closedir ( $file_pointer );

		return true;
	}


   // ! Executor

   /**
	* This is just a method used to abstract the file writing process.
	*
	* @param $name        Name of the file to write
	* @param $contents    The data to write to the new file
	* @param $destination The full path to the new file's destination
	* @param $chmod       File access permissions
	* @param $type        How to write the data to the file
	* @return Bool
	*/
	public function writeDataToFile ( $name, $contents, $destination, $chmod = 0655, $type = 'a' )
	{
		if ( false == is_dir ( $destination ) )
		{
			return false;
		}

		if ( false == is_writable ( $destination ) )
		{
			return false;
		}

		$file_path = $destination . '/' . $name;

		$file_pointer = fopen ( $file_path, $type );

		fwrite ( $file_pointer, $contents );
		fclose ( $file_pointer );

		chmod ( $file_path, $chmod );

		return true;
	}


   // ! Executor

   /**
	* This method will take the size of a file ( or any given
	* number ) and return the shorthand literal size notation
	*
	* @param $file_size Size of the file
	* @return String
	*/
	public function returnLiteralFileSize ( $file_size )
	{
		$sizes = array ( ' B', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB' );
		$ext   = $sizes[0];

		for ( $i=1; $i < count ( $sizes ) && $file_size >= 1024; $i++ )
		{
			$file_size = $file_size / 1024;
			$ext       = $sizes[$i];
		}

		return round ( $file_size, 2 ) . $ext;
	}


   // ! Executor

   /**
	* Takes an single-dimensional array and write it
	* to a specified file.
	*
	* @param $arr_data    Data to write to file
	* @param $arr_name    Name of the array
	* @param $name        Name of the file
	* @param $destination File destination
	* @return Bool
	*/
	public function updateArrayFile ( $arr_data, $arr_name, $name, $destination )
	{
		$contents = "<?php\n";

		foreach ( $arr_data as $key => $val )
		{
			$contents .= "\${$arr_name}['{$key}'] = '{$val}';\n";
		}

		$contents .= '?>';

		return FileHandlingClass::writeDataToFile ( $name, $contents, $destination, 0755, 'w' );
	}
}

?>